package ioperations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation{
public void work(BookList bookList){
    System.out.println("新增图书.......");
    //判断
    int currentsize=bookList.getUseSize();
    if(currentsize==bookList.getBooks().length){
        System.out.println("书架满了，不能放了");
        return;
    }
    //构建对象
    Scanner scanner=new Scanner(System.in);
    System.out.println("请输入书名");
    String name=scanner.nextLine();
    System.out.println("请输入书的作者");
    String author=scanner.nextLine();
    System.out.println("请输入价格");
    int price=scanner.nextInt();
    System.out.println("请输入书的类型");
    String type=scanner.nextLine();
    Book newbook=new Book(name,author,price,type);
   //判断暑假中有没有这本书
    for(int i=0;i<currentsize;i++) {
        Book book = bookList.getBooks(i);
        if (book.getName().equals(name)) {
            System.out.println("书架中有这本书，不能插入");
            System.out.println(book);
            return;
        }
    }
    //插入这本书
    bookList.setBooks(currentsize,newbook);
    bookList.setUseSize(currentsize+1);
    System.out.println("新增图书成功！！！");
}
}
